from django.db import models


class NFCDevice(models.Model):
    '''商户NFC碰一下设备'''

    IS_REGISTER_CHOICES = (
        (0, '否'),
        (1, '是')
    )


    IS_LIGHTEN_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    DEVICE_TYPE_CHOICES = (
        (1, '支付宝盒 N5H'),
        (2, '支付宝盒 N5D'),
        (3, '支付宝盒 N7HD'),
        (4, '支付宝盒 N7HM')
    )

    REACH_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    DISTINCT_USER_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    REWARDS_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.SET_NULL, null=True, blank=True)
    sn = models.CharField(verbose_name='sn码', max_length=32, unique=True)
    device_type = models.SmallIntegerField(verbose_name='设备类型', choices=DEVICE_TYPE_CHOICES, default=1)
    work_user = models.CharField(verbose_name='工作人员', max_length=32, null=True,blank=True)
    work_user_uid = models.CharField(verbose_name='工作人员uid', max_length=32, null=True,blank=True)
    is_register = models.SmallIntegerField(verbose_name='是否注册', choices=IS_REGISTER_CHOICES, default=0)
    register_time = models.DateTimeField(verbose_name='注册时间', null=True, blank=True)
    is_lighten = models.SmallIntegerField(verbose_name='是否点亮', choices=IS_LIGHTEN_CHOICES, default=0)
    lighten_time = models.DateTimeField(verbose_name='点亮时间', null=True, blank=True)
    store_name = models.CharField(verbose_name='门店名称', max_length=64, null=True, blank=True)
    address = models.CharField(verbose_name='门店地址', max_length=128, null=True, blank=True)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='所属机构', on_delete=models.SET_NULL, null=True, blank=True)
    alipay_num = models.IntegerField(verbose_name='支付宝交易笔数', default=0)
    total_amount = models.DecimalField(verbose_name='交易总金额', max_digits=10, decimal_places=2, default=0)
    nfc_num = models.IntegerField(verbose_name='支付宝交易笔数', default=0)
    nfc_amount = models.DecimalField(verbose_name='nfc金额', max_digits=10, decimal_places=2, default=0)
    reach_days = models.IntegerField(verbose_name='动销天数', default=0)
    remain_days = models.IntegerField(verbose_name='剩余天数', default=0)
    distinct_user_num = models.IntegerField(verbose_name='有效去重用户数', default=0)
    reach_status = models.SmallIntegerField(verbose_name='动销状态', choices=REACH_STATUS_CHOICES, default=0)
    distinct_user_status = models.SmallIntegerField(verbose_name='去重用户达标状态', choices=DISTINCT_USER_STATUS_CHOICES, default=0)
    rewards_status = models.SmallIntegerField(verbose_name='铺设奖励状态', choices=REWARDS_STATUS_CHOICES, default=0)
    reach_list = models.TextField(verbose_name='有效动销笔数', null=True, blank=True)
    iotRealRightId = models.CharField(verbose_name='有效动销笔数', max_length=64, null=True, blank=True)

    class Meta:
        db_table = 'tb_nfc_device'
        verbose_name = '商户NFC碰一下设备'
        verbose_name_plural = verbose_name


class NFCDeviceStatistics(models.Model):
    '''商户NFC碰一下设备统计'''

    IS_REACH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    REACH_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    DISTINCT_USER_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    REWARDS_STATUS_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    device = models.ForeignKey('alipaynfc.NFCDevice', verbose_name='设备', on_delete=models.CASCADE)
    trade_time = models.DateTimeField(verbose_name='交易时间')
    alipay_num = models.IntegerField(verbose_name='支付宝交易笔数', default=0)
    alipay_user_num = models.IntegerField(verbose_name='支付宝用户数', default=0)
    total_amount = models.DecimalField(verbose_name='交易总金额', max_digits=10, decimal_places=2, default=0)
    nfc_num = models.IntegerField(verbose_name='支付宝交易笔数', default=0)
    nfc_user_num = models.IntegerField(verbose_name='支付宝用户数', default=0)
    nfc_amount = models.DecimalField(verbose_name='nfc金额', max_digits=10, decimal_places=2, default=0)
    is_reach = models.SmallIntegerField(verbose_name='是否动销>3元',choices=IS_REACH_CHOICES, default=0)
    reach_days = models.IntegerField(verbose_name='动销天数', default=0)
    remain_days = models.IntegerField(verbose_name='剩余天数', default=0)
    distinct_user_num = models.IntegerField(verbose_name='有效去重用户数', default=0)
    reach_status = models.SmallIntegerField(verbose_name='动销状态', choices=REACH_STATUS_CHOICES, default=0)
    distinct_user_status = models.SmallIntegerField(verbose_name='去重用户达标状态', choices=DISTINCT_USER_STATUS_CHOICES, default=0)
    rewards_status = models.SmallIntegerField(verbose_name='铺设奖励状态', choices=REWARDS_STATUS_CHOICES, default=0)
    reach_list = models.TextField(verbose_name='有效动销笔数', null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    fault_days = models.IntegerField(verbose_name='容错天数', default=0)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    nfc_amount3_num= models.IntegerField(verbose_name='nfc交易>=3元的笔数', default=0)

    class Meta:
        db_table = 'tb_nfc_device_statistics'
        verbose_name = '商户NFC碰一下设备'
        verbose_name_plural = verbose_name


class NFCDeviceBountyQuest(models.Model):
    '''设备赏金任务'''

    QUEST_STATUS_CHOICES = (
        (0, '初始化'),
        (1, '进行中'),
        (2, '已完成'),
        (3, '已申诉'),
        (4, '已取消'),
        (5, '已过期'),
        (6, '申诉已处理')
    )

    VALIDITY_TYPE_CHOICES = (
        (1, '分'),
        (2, '时'),
        (3, '天')
    )

    task_id = models.CharField(verbose_name='任务id', max_length=32, unique=True)
    device = models.ForeignKey('alipaynfc.NFCDevice', verbose_name='设备id', on_delete=models.CASCADE)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name='赏金', max_digits=10, decimal_places=2, default=0)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    validity_type = models.SmallIntegerField(verbose_name='有效期类型', choices=VALIDITY_TYPE_CHOICES, default=1)
    validity_count = models.IntegerField(verbose_name='有效期长度')
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    quest_status = models.SmallIntegerField(verbose_name='任务状态', choices=QUEST_STATUS_CHOICES, default=0)
    create_time = models.DateTimeField(verbose_name='发布时间', auto_now_add=True)

    class Meta:
        db_table = 'tb_nfc_device_bounty_quest'
        verbose_name = '设备赏金任务'
        verbose_name_plural = verbose_name


class NFCDeviceBountyQuestSchedule(models.Model):
    '''赏金任务进度'''

    QUEST_STATUS_CHOICES = (
        (0, '初始化'),
        (1, '进行中'),
        (2, '已完成'),
        (3, '已申诉'),
        (4, '已取消')
    )

    APPEAL_STATUS_CHOICES = (
        (1, '待处理'),
        (2, '申诉通过'),
        (3, '申诉不通过')
    )

    quest = models.ForeignKey('alipaynfc.NFCDeviceBountyQuest', verbose_name='任务id', on_delete=models.CASCADE)
    quest_status = models.SmallIntegerField(verbose_name='任务状态', choices=QUEST_STATUS_CHOICES, default=1)
    create_time = models.DateTimeField(verbose_name='开始时间')
    expired_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    cancel_time = models.DateTimeField(verbose_name='取消时间', null=True, blank=True)
    appeal_time = models.DateTimeField(verbose_name='申诉时间', null=True, blank=True)
    success_time = models.DateTimeField(verbose_name='完成时间', null=True, blank=True)
    award = models.ForeignKey('cashbonus.CashActivityData', verbose_name='奖励', on_delete=models.SET_NULL, null=True, blank=True)
    appeal_explain = models.CharField(verbose_name='申诉说明', max_length=256, null=True, blank=True)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='领取用户', on_delete=models.CASCADE)
    appeal_status = models.SmallIntegerField(verbose_name='任务状态', choices=APPEAL_STATUS_CHOICES, default=1)
    handle_time = models.DateTimeField(verbose_name='处理申诉时间', null=True, blank=True)
    handle_explain = models.CharField(verbose_name='处理申诉说明', max_length=256, null=True, blank=True)

    class Meta:
        db_table = 'tb_nfc_device_bounty_quest_schedule'
        verbose_name = '赏金任务进度'
        verbose_name_plural = verbose_name


class NFCDeviceBountyQuestScheduleAppealAssist(models.Model):
    '''申诉材料'''

    schedule = models.ForeignKey('alipaynfc.NFCDeviceBountyQuestSchedule', verbose_name='任务id', on_delete=models.CASCADE)
    picture = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_nfc_device_bounty_quest_schedule_appeal_assist'
        verbose_name = '申诉材料'
        verbose_name_plural = verbose_name


class NFCDeviceBountyQuestSubscription(models.Model):
    '''赏金任务订阅通知'''

    IS_PUSH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='领取用户', on_delete=models.CASCADE)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='订阅时间', auto_now_add=True)
    is_push = models.SmallIntegerField(verbose_name='是否推送', choices=IS_PUSH_CHOICES, default=0)
    quest = models.ForeignKey('alipaynfc.NFCDeviceBountyQuest', verbose_name='任务id', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_nfc_device_bounty_quest_subscription'
        verbose_name = '赏金任务订阅通知'
        verbose_name_plural = verbose_name
